using System;
using System.Collections.Generic;
using System.Text;


using System.Windows.Forms;
using System.Drawing;


using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Drawing.Imaging;
using System.Collections;

using RoverStudio.Common;
using RoverStudio.Common.PositionSource;

namespace VideoSource
{

    public class VideoSourceException : Exception
    {
        public VideoSourceException(string message)
            : base(message)
        {
        }
    }


    public class VideoSource : IDisposable, IVideoSourceCommon
    {

        public event NewFrameDelegate NewFrame;

        private WebCamera webCamera;

        public VideoSource()
        {
            webCamera = new WebCamera(640, 480);
        }

        private void PictureGrabbed(Bitmap bitmap)
        {

            if (NewFrame != null)
                NewFrame(bitmap);
        }



        #region IDisposable Members

        public void Dispose()
        {
            if (webCamera != null)
                webCamera.CloseWebcam();
        }

        #endregion

        public void ShowVideoSource()
        {
            if (webCamera != null)
            {
                webCamera.ShowVideoSource();
                started = false;
            }

        }

        public void ShowVideoFormat()
        {
            if (webCamera != null)
            {
                webCamera.ShowConfigDialog();
                started = false;
            }
        }

        private bool started;

        public void Start()
        {
            if (started || webCamera == null)
                return;
            webCamera.RecievedFrame += new EventHandler<WebCameraEventArgs>(webCamera_RecievedFrame);
            webCamera.StartWebCam();
            started = true;

        }

        void webCamera_RecievedFrame(object sender, WebCameraEventArgs e)
        {
            this.PictureGrabbed(e.Bitmap);
        }

        public void Stop()
        {
            if (!started)
                return;
            webCamera.RecievedFrame -= new EventHandler<WebCameraEventArgs>(webCamera_RecievedFrame);
            webCamera.CloseWebcam();
            started = false;
        }

        public bool SetFramesPerSec(double fps)
        {
            bool result = webCamera.SetFramesPerSec(fps);
            if (!result)
                Stop();
            return result;
        }

        public double GetFramesPerSec()
        {
            return webCamera.GetFramesPerSec();
        }

        public bool SetFrameSize(int width, int height)
        {
            bool result = webCamera.SetFrameSize(new Size(width, height));
            Stop();
            if (result)
                Start();
            return result;
        }

        public Size GetFrameSize()
        {
            return webCamera.GetFrameSize();
        }
    }
}
